package com.wie.controller;

import com.wie.pojo.Category;
import com.wie.pojo.Result;
import com.wie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        log.info("新增文章分类：{}", category);
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 获取文章分类列表
     * @return
     */
    @GetMapping
    public Result<List<Category>> list() {
        log.info("获取文章分类列表");
        List<Category> categoryList = categoryService.list();
        return Result.success(categoryList);
    }

    /**
     * 根据文章id获取文章分类详情
     * @param categoryId
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer categoryId) {
        log.info("获取文章分类详情");
        Category category = categoryService.findCategoryByCategoryId(categoryId);
        return Result.success(category);
    }

    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Category category) {
        log.info("更新文章分类：{}", category);
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除文章分类：{}", id);
        categoryService.delete(id);
        return Result.success();
    }

}
